package com.android.nfctouch.ui.activity;

import android.content.Intent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.baselibrary.interface_.CommCallBack;
import com.android.baselibrary.interface_.OkHttpCallBack;
import com.android.baselibrary.responce.BaseResponce;
import com.android.baselibrary.tool.CommToast;
import com.android.baselibrary.util.Util;
import com.android.baselibrary.view.Comm_HeadView;
import com.android.baselibrary.view.GridSpacingItemDecoration;
import com.android.baselibrary.view.MultiStateView;
import com.android.nfctouch.R;
import com.android.nfctouch.adapter.ContactManagerAdapter;
import com.android.nfctouch.adapter.ContactTypeListAdapter;
import com.android.nfctouch.bean.ContactInfo;
import com.android.nfctouch.bean.ContactTypeInfo;
import com.android.nfctouch.constants.Constants;
import com.android.nfctouch.helper.ItemDragHelperCallback;
import com.android.nfctouch.manager.API_HomeManager;
import com.android.nfctouch.responce.BaseListData;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import butterknife.BindView;

/**
 *  管理
 */
public class MyContactManageActivity extends BaseActivity {

    @BindView(R.id.multiplestatusView)
    MultiStateView multiplestatusView;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.smartrefreshlayout)
    SmartRefreshLayout smartrefreshlayout;
    @BindView(R.id.comm_title)
    Comm_HeadView comm_title;

    ContactManagerAdapter adapter;
    int page = 1;
    int size = 20;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_onlylist;
    }

    @Override
    protected void initView() {
        comm_title.setTitle("联系方式管理");
        multiplestatusView.setOnRetryListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                multiplestatusView.setViewState(MultiStateView.ViewState.LOADING);
                getData();
            }
        });

        recyclerview.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerview.addItemDecoration(new GridSpacingItemDecoration(1, Util.dip2px(mContext, 10f), false));
        adapter = new ContactManagerAdapter(mContext, callBack);
        recyclerview.setAdapter(adapter);

        smartrefreshlayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                refreshData();
            }
        });
        smartrefreshlayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                loadMoreData();
            }
        });

        ItemDragHelperCallback callback = new ItemDragHelperCallback();
        final ItemTouchHelper helper = new ItemTouchHelper(callback);
        helper.attachToRecyclerView(recyclerview);
    }

    CommCallBack callBack = new CommCallBack() {
        @Override
        public void onResult(Object obj) {
            if(obj==null)
            {
                refreshData();
            }
            else
            {
                ContactInfo contactInfo = (ContactInfo) obj;
                Intent intent = new Intent(mContext,AddContactActivity.class);
                intent.putExtra("contactInfo",contactInfo);
                startActivity(intent);
            }
        }
    };

    @Override
    protected void onResume() {
        super.onResume();
        getData();
    }

    @Override
    protected void initData() {
    }

    //下拉刷新
    private void refreshData() {
        size = 20;
        getData();
    }

    //上拉加载
    private void loadMoreData() {
        size += 20;
        getData();
    }

    //获取数据
    private void getData() {
        API_HomeManager.home_contact_list(mContext, size, page, new OkHttpCallBack<BaseResponce<BaseListData<ContactInfo>>>() {
            @Override
            public void onSuccess(BaseResponce<BaseListData<ContactInfo>> baseResponce) {
                smartrefreshlayout.finishRefresh();
                smartrefreshlayout.finishLoadMore();
                adapter.setData(baseResponce.getData().list);
                if (adapter.getItemCount() == 0) {
                    multiplestatusView.setViewState(MultiStateView.ViewState.EMPTY);
                } else {
                    multiplestatusView.setViewState(MultiStateView.ViewState.CONTENT);
                }
            }

            @Override
            public void onFailure(BaseResponce<BaseListData<ContactInfo>> baseResponce) {
                smartrefreshlayout.finishRefresh();
                smartrefreshlayout.finishLoadMore();
                multiplestatusView.setViewState(MultiStateView.ViewState.ERROR);
                CommToast.showToast(mContext, Constants.NetError);
            }
        });
    }

    //点击事件
    //    @OnClick({R.id.tv_sendcode})
//    public void onViewClicked(View view) {
//        switch (view.getId()) {
//            case R.id.tv_sendcode:
//                break;
//        }
//    }
}
